package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.utils.Const;


import com.jianyun.jyzs.utils.RceLog;
import com.jianyun.jyzs.utils.UtilsStyle;

/**
 * Created by yangshijun on 2018/5/29.
 */

public class KaiPiaoActivity extends AppCompatActivity {


    private final static String TAG = "RceWebViewActivity";
    WebView webView;
    TextView titleTextView;
    private String mPrevUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rce_activity_webview);
        ImageButton backImgBtn = (ImageButton) findViewById(R.id.imgbtn_custom_nav_back);
        backImgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleTextView = (TextView) findViewById(R.id.tv_custom_nav_title);
//        TextView optionsTextView = (TextView) findViewById(R.id.tv_custom_nav_option);
//        if (!getIntent().getBooleanExtra(Const.ABOUT, false)) {
//            optionsTextView.setText(R.string.rce_user_guide);
//        }
        mPrevUrl = getIntent().getStringExtra(Const.URL);
        webView = (WebView) findViewById(R.id.webView);

        webView.setVerticalScrollbarOverlay(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setBuiltInZoomControls(true);
        if (Build.VERSION.SDK_INT > 11) {
            webView.getSettings().setDisplayZoomControls(false);
        }
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);

        webView.setWebViewClient(new OAWebViewClient());
        webView.setWebChromeClient(new OAWebChromeClient());
        webView.loadUrl(mPrevUrl);
    }

    class OAWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (mPrevUrl != null) {
                if (!mPrevUrl.equals(url)) {
                    if (!(url.toLowerCase().startsWith("http://") || url.toLowerCase().startsWith("https://"))) {
                        Intent intent = new Intent("android.intent.action.VIEW");
                        Uri content_url = Uri.parse(url);
                        intent.setData(content_url);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        try {
                            startActivity(intent);
                        } catch (Exception e) {
                            RceLog.e(TAG, "not apps install for this intent =" + e.toString());
                            e.printStackTrace();
                        }
                        return true;
                    }
                    mPrevUrl = url;
                    webView.loadUrl(url);
                    return true;
                } else {
                    return false;
                }
            } else {
                mPrevUrl = url;
                webView.loadUrl(url);
                return true;
            }
        }
    }

    private class OAWebChromeClient extends WebChromeClient {

        @Override
        public void onReceivedTitle(WebView view, String title) {
            if (titleTextView != null && !TextUtils.isEmpty(title)) {
                titleTextView.setText(title);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }



}
